package com.ruoyi.teacher.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.teacher.domain.TCopyrightAuthor;
import com.ruoyi.teacher.mapper.TCopyrightMapper;
import com.ruoyi.teacher.domain.TCopyright;
import com.ruoyi.teacher.service.ITCopyrightService;

/**
 * 著作权信息集Service业务层处理
 *
 * @author xiangzy
 * @date 2022-05-10
 */
@Service
public class TCopyrightServiceImpl implements ITCopyrightService
{
    @Autowired
    private TCopyrightMapper tCopyrightMapper;

    /**
     * 查询著作权信息集
     *
     * @param recordid 著作权信息集ID
     * @return 著作权信息集
     */
    @Override
    public TCopyright selectTCopyrightById(Long recordid)
    {
        return tCopyrightMapper.selectTCopyrightById(recordid);
    }

    /**
     * 查询著作权信息集列表
     *
     * @param tCopyright 著作权信息集
     * @return 著作权信息集
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<TCopyright> selectTCopyrightList(TCopyright tCopyright)
    {
        return tCopyrightMapper.selectTCopyrightList(tCopyright);
    }

    /**
     * 新增著作权信息集
     *
     * @param tCopyright 著作权信息集
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTCopyright(TCopyright tCopyright)
    {
        tCopyright.setCreateTime(DateUtils.getNowDate());
        int rows = tCopyrightMapper.insertTCopyright(tCopyright);
        insertTCopyrightAuthor(tCopyright);
        return rows;
    }

    /**
     * 修改著作权信息集
     *
     * @param tCopyright 著作权信息集
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTCopyright(TCopyright tCopyright)
    {
        tCopyright.setUpdateTime(DateUtils.getNowDate());
        tCopyrightMapper.deleteTCopyrightAuthorByCOPYRIGHTID(tCopyright.getRecordid());
        insertTCopyrightAuthor(tCopyright);
        return tCopyrightMapper.updateTCopyright(tCopyright);
    }

    /**
     * 批量删除著作权信息集
     *
     * @param recordids 需要删除的著作权信息集ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTCopyrightByIds(Long[] recordids)
    {
        tCopyrightMapper.deleteTCopyrightAuthorByCOPYRIGHTIDs(recordids);
        return tCopyrightMapper.deleteTCopyrightByIds(recordids);
    }

    /**
     * 删除著作权信息集信息
     *
     * @param recordid 著作权信息集ID
     * @return 结果
     */
    @Override
    public int deleteTCopyrightById(Long recordid)
    {
        tCopyrightMapper.deleteTCopyrightAuthorByCOPYRIGHTID(recordid);
        return tCopyrightMapper.deleteTCopyrightById(recordid);
    }

    /**
     * 新增著作作者信息集信息
     *
     * @param tCopyright 著作权信息集对象
     */
    public void insertTCopyrightAuthor(TCopyright tCopyright)
    {
        List<TCopyrightAuthor> tCopyrightAuthorList = tCopyright.getTCopyrightAuthorList();
        Long recordid = tCopyright.getRecordid();
        if (StringUtils.isNotNull(tCopyrightAuthorList))
        {
            List<TCopyrightAuthor> list = new ArrayList<TCopyrightAuthor>();
            for (TCopyrightAuthor tCopyrightAuthor : tCopyrightAuthorList)
            {
                tCopyrightAuthor.setCopyrightid(recordid);
                list.add(tCopyrightAuthor);
            }
            if (list.size() > 0)
            {
                tCopyrightMapper.batchTCopyrightAuthor(list);
            }
        }
    }
}
